package com.bangyu.monitor.modular.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.bangyu.monitor.core.common.annotion.Permission;
import com.bangyu.monitor.core.common.exception.BizExceptionEnum;
import com.bangyu.monitor.core.common.page.LayuiPageFactory;
import com.bangyu.monitor.core.log.LogObjectHolder;
import com.bangyu.monitor.modular.system.entity.Bridge;
import com.bangyu.monitor.modular.system.entity.MaintainRecord;
import com.bangyu.monitor.modular.system.model.MaintainRecordDto;
import com.bangyu.monitor.modular.system.service.MaintainRecordService;
import com.bangyu.monitor.modular.system.warpper.MaintainRecordWrapper;
import com.bangyu.monitor.modular.system.warpper.UserWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @Auther: zhangxue
 * @Date: 19/4/8 16:15
 * @Description:
 */
@Controller
@RequestMapping("/maintainRecord")
public class MaintainRecordController extends BaseController {

    private static String PREFIX = "/modular/system/maintain_record/";


    @Autowired
    private MaintainRecordService recordService;


    /**
     * 跳转到字典管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "record.html";
    }

    /**
     * 跳转到添加字典管理
     */
    @RequestMapping("/record_add")
    public String MaintainRecordAdd() {
        return PREFIX + "record_add.html";
    }

//    /**
//     * 跳转到修改字典管理
//     */
//    @RequestMapping("/record_update/{recordId}")
//    public String recordUpdate(@PathVariable Integer recordId, Model model) {
//        MaintainRecord record = recordService.selectById(recordId);
//        model.addAttribute("item",record);
//        LogObjectHolder.me().set(record);
//        return PREFIX + "record_edit.html";
//    }
//
    /**
     * 获取维修记录
     */
    @RequestMapping(value = "/pageAll")
    @ResponseBody
    public Object pageAll(@RequestParam(value = "bridgeName",required = false) String bridgeName) {
        Page<Map<String, Object>> page = recordService.pageAll(bridgeName);
        Page wrapped = new MaintainRecordWrapper(page).wrap();
        return LayuiPageFactory.createPageInfo(wrapped);
    }

    /**
     * 新增维修记录
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(MaintainRecordDto dto) {
        recordService.addRecord(dto);
        return SUCCESS_TIP;
    }

    /**
     * 跳转到修改角色
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:31 PM
     */
    @Permission
    @RequestMapping(value = "/record_edit")
    public String recordEdit(@RequestParam Long id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        MaintainRecord record = this.recordService.getById(id);
        LogObjectHolder.me().set(record);
        return PREFIX + "record_edit.html";
    }

    @RequestMapping(value = "/view/{id}")
    @ResponseBody
    public ResponseData view(@PathVariable Long id) {
        if (ToolUtil.isEmpty(id)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        MaintainRecord record = this.recordService.getById(id);
        Map<String, Object> bridgeMap = BeanUtil.beanToMap(MaintainRecordDto.moduleToDto(record));

        return ResponseData.success(bridgeMap);
    }
//
//    /**
//     * 删除字典管理
//     */
//    @RequestMapping(value = "/delete")
//    @ResponseBody
//    public Object delete(@RequestParam Integer recordId) {
//        recordService.deleteById(recordId);
//        return SUCCESS_TIP;
//    }
//
    /**
     * 修改维修日志
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(MaintainRecordDto dto) {
        recordService.update(dto);
        return SUCCESS_TIP;
    }

//    /**
//     * 字典管理详情
//     */
//    @RequestMapping(value = "/detail/{recordId}")
//    @ResponseBody
//    public Object detail(@PathVariable("recordId") Integer recordId) {
//        return recordService.selectById(recordId);
//    }


}
